﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WinLib.Common;
namespace YNLibWin
{
    public partial class frmRecycle : Form
    {
        public frmRecycle()
        {
            InitializeComponent();
        }

        private void frmRecycle_Load(object sender, EventArgs e)
        {
            InitInfo();
            BindData();
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected void InitInfo()
        {
            #region 绑定用户检索字段
            ComboBoxItem cbi1 = new ComboBoxItem();
            cbi1.Text = "用户账号";
            cbi1.Value = "登录账号";
            this.cboSearchField.Items.Add(cbi1);
            ComboBoxItem cbi2 = new ComboBoxItem();
            cbi2.Text = "用户姓名";
            cbi2.Value = "用户姓名";
            this.cboSearchField.Items.Add(cbi2);
            this.cboSearchField.SelectedIndex = 0;
            #endregion
        }
        private void BindData()
        {
            BindData("");
        }
        private void BindData(string filter)
        {
            string sqlstr = string.Format("select SYSID as ID,UserCode as '登录账号',RealName as '用户姓名',UserFlag as '当前状态',Email,JoinDate as '开户时间',EndDate as '到期时间' from [SSO_UserBaseInfo] where RECFLAG=2");
            DataTable dt = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlstr);
            if (!string.IsNullOrEmpty(filter))
            {
                DataTable newdt = new DataTable();
                newdt = dt.Clone();
                DataRow[] dr = dt.Select(filter);
                for (int i = 0; i < dr.Length; i++)
                {
                    newdt.ImportRow((DataRow)dr[i]);
                }
                this.dataGridView1.DataSource = newdt;
            }
            else
            {
                this.dataGridView1.DataSource = dt;
            }

            this.lblCount.Text = "共" + dt.Rows.Count.ToString() + "条记录";
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认彻底删除选中的行吗？删除后不可恢复", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int intResult = 0;
                int intCountRows = this.dataGridView1.Rows.Count;
                for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--)
                {
                    int ID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells[0].Value);
                    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);
                    string sqlstr = string.Format("delete from [SSO_UserBaseInfo] where SYSID=" + ID.ToString());
                    int intDel = Convert.ToInt32(WinLib.DBUtility.DbHelperSQL.ExecuteNonQuery(sqlstr)); //cl是操作类的一个对像，Execute()是类中的一个方法
                    if (intDel > 0)
                    {
                        intResult += intDel;
                    }
                }
                int intCount = intCountRows - intResult;
                this.lblCount.Text = "共" + intCount.ToString() + "条记录";
                MessageBox.Show("成功删除" + intResult.ToString() + "条记录", "删除记录", MessageBoxButtons.OK);
            }
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认恢复选中的行吗？", "恢复记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int intResult = 0;
                int intCountRows = this.dataGridView1.Rows.Count;
                for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--)
                {
                    int ID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells[0].Value);
                    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);
                    string sqlstr = string.Format("Update [SSO_UserBaseInfo] set RECFLAG=1 where SYSID=" + ID.ToString());
                    int intDel = Convert.ToInt32(WinLib.DBUtility.DbHelperSQL.ExecuteNonQuery(sqlstr)); //cl是操作类的一个对像，Execute()是类中的一个方法
                    if (intDel > 0)
                    {
                        intResult += intDel;
                    }
                }
                int intCount = intCountRows - intResult;
                this.lblCount.Text = "共" + intCount.ToString() + "条记录";
                MessageBox.Show("成功恢复" + intResult.ToString() + "条记录", "恢复记录", MessageBoxButtons.OK);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtKeyword.Text))
            {
                MessageBox.Show("请输入检索关键词");
                return;
            }
            string strField = Convert.ToString(((ComboBoxItem)this.cboSearchField.SelectedItem).Value);
            string strFilter = strField + " like '%" + this.txtKeyword.Text.Trim() + "%'";
            BindData(strFilter);
        }

    }
}
